/*
 * @(#)MaintSysUserDefinedIndex.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.framework.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.bean.SysUdfFieldValue;

/**
  MaintSysUdfFieldValuexAction.java
  This class is for maint system user defined index.
    @author          Phoebe Wong
    @company         DCIVision Limited
    @creation date   01/08/2003
    @version         $Revision: 1.6.2.1 $
 */

public class MaintSysUdfFieldValueAction extends AbstractMaintAction {

  public static final String REVISION = "$Revision: 1.6.2.1 $";

  protected String listName = "";

  public MaintSysUdfFieldValueAction() {
    super();
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  
  public String getMajorDAOClassName() {
    return ("com.dcivision.framework.dao.SysUdfFieldValueDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  
  public String getFunctionCode() {
    return (SystemFunctionConstant.DMS_DOC_PROFILE);
  }

  
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
    AbstractActionForm maintForm = (AbstractActionForm)form;

    String opMode = (maintForm != null) ? maintForm.getOpMode() : request.getParameter("opMode"); //maintForm.getOpMode();
    String navMode = (maintForm != null) ? maintForm.getNavMode() : request.getParameter("navMode"); //maintForm.getNavMode();

    if (GlobalConstant.OP_MODE_REFRESH.equals(opMode)) {
      // Set the function code for current action.
      ActionForward forward = this.retrieveFunctionCode(request, response, mapping);
      if (forward != null) {
        return forward;
      }
      refreshFormData(mapping, maintForm, request, response);
      return mapping.findForward(GlobalConstant.NAV_MODE_REFRESH);
    }
    else {
      log.debug("opMode: " + opMode + ", navMode: " + navMode);
      return super.execute(mapping, form, request, response);
    }
  }

  public void refreshFormData(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws
    ApplicationException {
    List resultList = new ArrayList();

    try {
      SessionContainer sessionCon = this.getSessionContainer(request);
      MaintSysUdfFieldValueForm udfFieldValueForm = (MaintSysUdfFieldValueForm)form;
      int listSize = 0;
      int intNewField = 0;
      try {
        listSize = TextUtility.parseInteger(request.getParameter("listSize"));
        intNewField = TextUtility.parseInteger(request.getParameter("newField"));
      } catch (Exception e) {
        throw new ApplicationException(ErrorConstant.COMMON_FATAL_ERROR, e);
      }

      int sum = 0;
      sum = listSize + intNewField;
      //Set Header Form data
      udfFieldValueForm.setFormData(udfFieldValueForm.getFormData());

      for (int i = 0; i < listSize; i++) {
        SysUdfFieldValue tmpSysUdfFieldValue = new SysUdfFieldValue();
        if (! ("".equals(request.getParameter("definedFieldValue_" + i))) && ! ("null".equals(request.getParameter("definedFieldValue_" + i)))) {
          tmpSysUdfFieldValue.setFieldValue(request.getParameter("definedFieldValue_" + i));
        }
        
        String strDeleteCheckValue = request.getParameter("delete_" + i);
        if (strDeleteCheckValue == null) {
          resultList.add(tmpSysUdfFieldValue);
        }
      }
      udfFieldValueForm.setFieldList(resultList);

      request.setAttribute("listSize", new Integer(listSize));
      request.setAttribute("newFieldsSize", new Integer(sum));
      request.setAttribute("newField", new Integer(intNewField));
    } catch (ApplicationException appEx) {
      throw appEx;
    } catch (Exception e) {
      log.error(e, e);
      throw new ApplicationException(ErrorConstant.COMMON_FATAL_ERROR, e);
    } finally {
    }
  }

}
